package com.edu.hre.struts.action.pim;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.db.pojo.HreEmployee;
import com.edu.hre.db.pojo.HreUser;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmployeePersonalForm;

public class EmployeePersonalAction extends DispatchAction {

	private static final Logger logger = Logger.getLogger(EmployeePersonalAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "employeePersonal";
 
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// Print source code location.
		CommonUtils.println(CommonUtils.getSourceCodeLocation());

		EmployeePersonalForm employeeForm = (EmployeePersonalForm) form;
		
		HreUser tmpUser = (HreUser) request.getSession().getAttribute(RequestUtils.Attribute.USER_INFO);
		tmpUser = (HreUser) baseService.getFirstPojo("id", Compare.EQUAL, tmpUser.getId(), null, null, 0, HreUser.class);

		if (tmpUser == null) {
			return null;
		}
		
		HreEmployee tmpEmp = (HreEmployee) baseService.getFirstPojo("empNumber", Compare.EQUAL, tmpUser.getEmpNumber(), null, null, 0, HreEmployee.class);
		if (tmpEmp == null) {
			return null;
		}

		employeeForm.setEmpNumber(tmpUser.getEmpNumber() + "");
		response.sendRedirect(request.getContextPath() + "/empPersonalDetailUpdate.do?id=" + tmpUser.getEmpNumber());
		return null;
	}
}
